package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.TFileInfo;
import com.ruoyi.system.domain.bo.TFileInfoBo;
import com.ruoyi.system.domain.vo.TFileInfoVo;
import com.ruoyi.system.mapper.TFileInfoMapper;
import com.ruoyi.system.service.ITFileInfoService;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * 图片文件保存Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-11
 */
@RequiredArgsConstructor
@Service
public class TFileInfoServiceImpl implements ITFileInfoService {

    private final TFileInfoMapper baseMapper;

    /**
     * 查询图片文件保存
     */
    @Override
    public TFileInfoVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询图片文件保存列表
     */
    @Override
    public TableDataInfo<TFileInfoVo> queryPageList(TFileInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TFileInfo> lqw = buildQueryWrapper(bo);
        Page<TFileInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询图片文件保存列表
     */
    @Override
    public List<TFileInfoVo> queryList(TFileInfoBo bo) {
        LambdaQueryWrapper<TFileInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TFileInfo> buildQueryWrapper(TFileInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TFileInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getBusiId() != null, TFileInfo::getBusiId, bo.getBusiId());
        lqw.eq(StringUtils.isNotBlank(bo.getBusiType()), TFileInfo::getBusiType, bo.getBusiType());
        lqw.eq(StringUtils.isNotBlank(bo.getUrl()), TFileInfo::getUrl, bo.getUrl());
        lqw.like(StringUtils.isNotBlank(bo.getName()), TFileInfo::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getFileFormat()), TFileInfo::getFileFormat, bo.getFileFormat());
        lqw.eq(StringUtils.isNotBlank(bo.getFileType()), TFileInfo::getFileType, bo.getFileType());
        lqw.eq(bo.getCreateId() != null, TFileInfo::getCreateId, bo.getCreateId());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), TFileInfo::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增图片文件保存
     */
    @Override
    public Boolean insertByBo(TFileInfoBo bo) {
        TFileInfo add = BeanUtil.toBean(bo, TFileInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改图片文件保存
     */
    @Override
    public Boolean updateByBo(TFileInfoBo bo) {
        TFileInfo update = BeanUtil.toBean(bo, TFileInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TFileInfo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除图片文件保存
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<TFileInfo> fileList(String busId, String busType) {
        List<TFileInfo> list = Lists.newArrayList();
        LambdaQueryWrapper<TFileInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TFileInfo::getDelFlag, "0");
        queryWrapper.eq(TFileInfo::getBusiType, busType);
        queryWrapper.eq(TFileInfo::getBusiId, busId);
        List<TFileInfo> tFileInfos = baseMapper.selectList(queryWrapper);
        if (!CollectionUtils.isEmpty(tFileInfos)) {
            return tFileInfos;
        }
        return list;
    }

    @Override
    public void fileList(String busId, String busType, List<TFileInfo> list) {
        if (!CollectionUtils.isEmpty(list)) {
            for (TFileInfo fileInfo : list) {
                if (!"1".equals(fileInfo.getDelFlag())) {
                    if (fileInfo.getId() != null) {
                        continue;
                    }
                    fileInfo.setStatus("1");
                    fileInfo.setDelFlag("0");
                    fileInfo.setId(null);
                    fileInfo.setBusiId(Long.valueOf(busId));
                    fileInfo.setBusiType(busType);
                    baseMapper.insert(fileInfo);
                }
            }
        }
    }
}
